﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Eniux.Windows.Dock
{
    using System;
    using System.Windows;

    /// <summary>
    /// 路由事件帮助类
    /// </summary>
    internal static class RoutedEventHelper
    {
        #region RoutedEvent Helper Methods

        /// <summary>
        /// 引发事件
        /// </summary>
        /// <param name="target">源</param>
        /// <param name="args">参数</param>
        internal static void RaiseEvent(DependencyObject target, RoutedEventArgs args)
        {
            if (target is UIElement)
            {
                (target as UIElement).RaiseEvent(args);
            }
            else if (target is ContentElement)
            {
                (target as ContentElement).RaiseEvent(args);
            }
        }

        /// <summary>
        ///  添加事件
        /// </summary>
        /// <param name="element">源</param>
        /// <param name="routedEvent">路由事件</param>
        /// <param name="handler"></param>
        internal static void AddHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
        {
            UIElement uie = element as UIElement;
            if (uie != null)
            {
                uie.AddHandler(routedEvent, handler);
            }
            else
            {
                ContentElement ce = element as ContentElement;
                if (ce != null)
                {
                    ce.AddHandler(routedEvent, handler);
                }
            }
        }

        /// <summary>
        /// 移除事件
        /// </summary>
        /// <param name="element">源</param>
        /// <param name="routedEvent">路由事件</param>
        /// <param name="handler"></param>
        internal static void RemoveHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
        {
            UIElement uie = element as UIElement;
            if (uie != null)
            {
                uie.RemoveHandler(routedEvent, handler);
            }
            else
            {
                ContentElement ce = element as ContentElement;
                if (ce != null)
                {
                    ce.RemoveHandler(routedEvent, handler);
                }
            }
        }

        #endregion
    }
}
